﻿ 

USE [CodeGenerator]
GO
/****** Object:  StoredProcedure [shell].[Template_Delete]    Script Date: 16.11.2011 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Create date: 16.11.2011
-- Description:	Template Get By ID
-- =============================================
ALTER PROCEDURE [shell].[Template_Delete]
@Id bigint = NULL
AS
BEGIN
  SET NOCOUNT ON;

  DELETE FROM [shell].[Template]
  WHERE [shell].[Template].[Id] = @Id
  
END

GO
/****** Object:  StoredProcedure [shell].[Template_GetAll]    Script Date: 16.11.2011 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Create date: 16.11.2011
-- Description:	Template Get All
-- =============================================
ALTER PROCEDURE [shell].[Template_GetAll] 
@Id bigint = NULL
,	@Name nvarchar(1024) = NULL
,	@Type nvarchar(1024) = NULL
,	@Alias nvarchar(1024) = NULL
,	@Version int = NULL
,	@Value nvarchar(MAX) = NULL
,	@Comment nvarchar(MAX) = NULL
,	@Date datetime = NULL
,	@Key nvarchar(256) = NULL
AS
BEGIN
  SET NOCOUNT ON;

  SELECT * 
	FROM [shell].[Template]
  WHERE		(@Id IS NULL OR [Id] = @Id) 
		AND	(@Name IS NULL OR [Name] = @Name) 
		AND	(@Type IS NULL OR [Type] = @Type) 
		AND	(@Alias IS NULL OR [Alias] = @Alias) 
		AND	(@Version IS NULL OR [Version] = @Version) 
		AND	(@Value IS NULL OR [Value] = @Value) 
		AND	(@Comment IS NULL OR [Comment] = @Comment) 
		AND	(@Date IS NULL OR [Date] = @Date) 
		AND	(@Key IS NULL OR [Key] = @Key) 
END

GO

/****** Object:  StoredProcedure [shell].[Template_Count]    Script Date: 16.11.2011 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Create date: 16.11.2011
-- Description:	Template Get All
-- =============================================
ALTER PROCEDURE [shell].[Template_Count] 
@Id bigint = NULL
,	@Name nvarchar(1024) = NULL
,	@Type nvarchar(1024) = NULL
,	@Alias nvarchar(1024) = NULL
,	@Version int = NULL
,	@Value nvarchar(MAX) = NULL
,	@Comment nvarchar(MAX) = NULL
,	@Date datetime = NULL
,	@Key nvarchar(256) = NULL
AS
BEGIN
  SET NOCOUNT ON;

  DECLARE @count INT
  
  SELECT @count = COUNT([Id]) 
	FROM [shell].[Template]
  WHERE		(@Id IS NULL OR [Id] = @Id) 
		AND	(@Name IS NULL OR [Name] = @Name) 
		AND	(@Type IS NULL OR [Type] = @Type) 
		AND	(@Alias IS NULL OR [Alias] = @Alias) 
		AND	(@Version IS NULL OR [Version] = @Version) 
		AND	(@Value IS NULL OR [Value] = @Value) 
		AND	(@Comment IS NULL OR [Comment] = @Comment) 
		AND	(@Date IS NULL OR [Date] = @Date) 
		AND	(@Key IS NULL OR [Key] = @Key) 
	
	RETURN @count
END

GO
/****** Object:  StoredProcedure [shell].[Template_GetById]    Script Date: 16.11.2011 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Create date: 16.11.2011
-- Description:	Template Get By ID
-- =============================================
ALTER PROCEDURE [shell].[Template_GetById]
@Id bigint = NULL
AS
BEGIN
  SET NOCOUNT ON;

  
	DECLARE @err_message nvarchar(255)  
  DECLARE @count tinyint
  
  SELECT @count = COUNT([Id]) 
	FROM [shell].[Template]
	WHERE [shell].[Template].[Id] = @Id
  
  IF (@count = 1)
	BEGIN
		SELECT * 
		FROM [shell].[Template]
		WHERE [shell].[Template].[Id] = @Id
  END
  ELSE IF (@count = 0)
  BEGIN
		SET @err_message = '[shell].[Template_GetById] could not find [Id] = ' + CAST(@Id AS NVARCHAR(36))
		RAISERROR (@err_message, 10,1)
  END
  ELSE
  BEGIN
		SET @err_message = '[shell].[Template_GetById] found any [Id] = ' + CAST(@Id AS NVARCHAR(36))
		RAISERROR (@err_message, 10,1)
  END

END


GO
/****** Object:  StoredProcedure [shell].[Template_Insert]    Script Date: 16.11.2011 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Create date: 16.11.2011
-- Description:	Template Get By ID
-- =============================================
ALTER PROCEDURE [shell].[Template_Insert]
@Name nvarchar(1024) = NULL
, @Type nvarchar(1024) = NULL
, @Alias nvarchar(1024) = NULL
, @Version int = NULL
, @Value nvarchar(MAX) = NULL
, @Comment nvarchar(MAX) = NULL
, @Date datetime = NULL
, @Key nvarchar(256) = NULL
AS
BEGIN
  SET NOCOUNT ON;

  INSERT INTO	[shell].[Template]
  ([Name], [Type], [Alias], [Version], [Value], [Comment], [Date], [Key])
  VALUES
  (@Name, @Type, @Alias, @Version, @Value, @Comment, @Date, @Key)


    DECLARE @IDENTITY bigint
	SELECT @IDENTITY = @@IDENTITY
    
    EXEC [shell].[Template_GetById] @Id = @IDENTITY

    

END

GO

/****** Object:  StoredProcedure [shell].[Template_Update]    Script Date: 16.11.2011 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Create date: 16.11.2011
-- =============================================
ALTER PROCEDURE [shell].[Template_Update]
@Id bigint= NULL
, @Name nvarchar(1024)= NULL
, @Type nvarchar(1024)= NULL
, @Alias nvarchar(1024)= NULL
, @Version int= NULL
, @Value nvarchar(MAX)= NULL
, @Comment nvarchar(MAX)= NULL
, @Date datetime= NULL
, @Key nvarchar(256)= NULL
AS
BEGIN
  SET NOCOUNT ON;

  UPDATE [shell].[Template]
  SET [Name] = @Name
		, [Type] = @Type
		, [Alias] = @Alias
		, [Version] = @Version
		, [Value] = @Value
		, [Comment] = @Comment
		, [Date] = @Date
		, [Key] = @Key
  WHERE	[Id] = @Id 
    EXEC [shell].[Template_GetById] @Id = @Id

  
END

GO